import { readConfiguration } from './function/function.js'

export default class Button {
    constructor() {
        this.plugin = {
            name: 'Bread-Shop',
            dsc: '面包商店',
            priority: 1,
            rule: [
                { reg: /^[#\/][买吃啃送抢赌].*/, fnc: 'controls' },
                { reg: /^[#\/](.*)记录(买|[吃啃]|抢|(赠)?送|猜拳|赌)?/, fnc: 'breadRecord' },
                { reg: /^[#\/]查看.*/, fnc: 'viewBread' },
                { reg: /^[#\/]设置本群自定义物品.*/, fnc: 'setCustomItems' },
                { reg: /^[#\/]?(面包(商店)?|Bs)(菜单|帮助)$/, fnc: 'help' }
            ]
        }
    }

    async controls(e) {
        const ID = [e.group_id]
        const USER_INPUT_CONTENT = r(e.msg)
        const configData = await readConfiguration()
        let defaultItem = configData['bread_thing']
        const CUSTOM_ITEMS = configData['special_thing_group'][ID[0]]
        if (!CUSTOM_ITEMS && USER_INPUT_CONTENT !== defaultItem || CUSTOM_ITEMS && USER_INPUT_CONTENT !== CUSTOM_ITEMS) return false
        if (CUSTOM_ITEMS) defaultItem = CUSTOM_ITEMS

        return segment.button([
            [bd(0, '买', `#买${defaultItem}`), bd(0, '吃', `#吃${defaultItem}`), bd(0, '抢', `#抢${defaultItem}`), bd(0, '送', `#送${defaultItem}`)],
            [bd(0, '赌石头', `#赌${defaultItem}石头`), bd(0, '赌剪刀', `#赌${defaultItem}剪刀`), bd(0, '赌布', `#赌${defaultItem}布`)],
            [bd(0, `${defaultItem}榜`, `#${defaultItem}排行榜`), bd(0, `${defaultItem}全服榜`, `#${defaultItem}全局排行榜`)],
            [bd(0, '操作记录', `#${defaultItem}记录`), bd(0, `查看${defaultItem}`, `#查看${defaultItem}`), bd(1, '设置', '#设置本群自定义物品')]
        ])
    }

    async breadRecord(e) {
        const ID = [e.group_id]
        const [, , USER_INPUT_CONTENT, OPERATION] = e.msg.match(/^(#|\/)(.*)记录(买|[吃啃]|抢|(赠)?送|猜拳|赌)?$/)
        const configData = await readConfiguration()
        let defaultItem = configData['bread_thing']
        const CUSTOM_ITEMS = configData['special_thing_group'][ID[0]]
        if (!CUSTOM_ITEMS && USER_INPUT_CONTENT !== defaultItem || CUSTOM_ITEMS && USER_INPUT_CONTENT !== CUSTOM_ITEMS) return false
        if (CUSTOM_ITEMS) defaultItem = CUSTOM_ITEMS

        if (OPERATION) {
            return segment.button([
                [bd(2, `#${defaultItem}记录买`), bd(2, `#${defaultItem}记录吃`)],
                [bd(2, `#${defaultItem}记录抢`), bd(2, `#${defaultItem}记录送`)],
                [bd(2, `#${defaultItem}记录赌`)]
            ])
        } else {
            return segment.button([
                [bd(2, `#${defaultItem}记录买`), bd(2, `#${defaultItem}记录吃`)],
                [bd(2, `#${defaultItem}记录抢`), bd(2, `#${defaultItem}记录送`)],
                [bd(2, `#${defaultItem}记录赌`)]
            ])
        }
    }

    async viewBread(e) {
        const ID = [e.group_id]
        const USER_INPUT_CONTENT = r(e.msg)
        const configData = await readConfiguration()
        let defaultItem = configData['bread_thing']
        const CUSTOM_ITEMS = configData['special_thing_group'][ID[0]]
        if (!CUSTOM_ITEMS && USER_INPUT_CONTENT !== defaultItem || CUSTOM_ITEMS && USER_INPUT_CONTENT !== CUSTOM_ITEMS) return false
        if (CUSTOM_ITEMS) defaultItem = CUSTOM_ITEMS

        return segment.button([
            [bd(2, `#查看${defaultItem}`)],
            [bd(1, '设置个人昵称', `#设置${defaultItem}店昵称`)]
        ])
    }

    async setCustomItems() {
        return segment.button([[bd(2, '#面包帮助')]])
    }
    async help(e) {
        const ID = [e.group_id]
        const configData = await readConfiguration()
        let defaultItem = configData['bread_thing']
        const CUSTOM_ITEMS = configData['special_thing_group'][ID[0]]
        if (CUSTOM_ITEMS) defaultItem = CUSTOM_ITEMS

        return segment.button([
            [bd(0, '买', `#买${defaultItem}`), bd(0, '吃', `#吃${defaultItem}`), bd(0, '抢', `#抢${defaultItem}`), bd(0, '送', `#送${defaultItem}`)],
            [bd(0, '赌石头', `#赌${defaultItem}石头`), bd(0, '赌剪刀', `#赌${defaultItem}剪刀`), bd(0, '赌布', `#赌${defaultItem}布`)],
            [bd(0, `${defaultItem}榜`, `#${defaultItem}排行榜`), bd(0, `${defaultItem}全服榜`, `#${defaultItem}全局排行榜`)],
            [bd(0, '操作记录', `#${defaultItem}记录`), bd(0, `查看${defaultItem}`, `#查看${defaultItem}`)]
        ])
    }
}

/**
 * 按钮
 * @param {number} TYPE 0: 立即发送 | 1: 不立即发送 | 2: 直接发送外显 | 3: 链接
 * @param {string} $1 外显
 * @param {string} $2 数据
 * @returns {object}
 */
function bd(TYPE, text, data) {
    return {
        text,
        ...(TYPE === 0 && { callback: data }),
        ...(TYPE === 1 && { data }),
        ...(TYPE === 2 && { enter: true }),
        ...(TYPE === 3 && { link: data })
    };
}

function r(msg) {
    return msg.replace(/#|\/|买|吃|啃|送|赌|抢|查看/g, '')
}